# marshmallow 映射文件
from marshmallow import fields
from marshmallow_sqlalchemy import ModelSchema

from application import db
from application.users.models import Users, Class


class ClassSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Class
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    name = fields.String(required=True, error_messages={
        'required': 'classname required'
    })


class UsersSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Users
        sqla_session = db.session

    # dump_only:只允许接口读取该数据(只允许从model序列化成字典，不能反序列化)
    # load_only:与dump_only相反
    id = fields.Integer(dump_only=True)
    username = fields.String(required=True, error_messages={
        'required': 'username required'
    })
    fullname = fields.String(required=True, error_messages={
        'required': 'fullname required'
    })
    password = fields.String(required=True, load_only=True)
    status = fields.Integer()
    create_time = fields.DateTime(dump_only=True)
    current_listen_status = fields.String()
    avatar = fields.String(dump_only=True)
    # 嵌套json
    uclass = fields.Nested(ClassSchema)
